<template>
  <div>
    <el-dialog title="编辑学生" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item size="mini"  label="学生姓名" >
          <el-input size="mini" v-model="editForm.studentName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item size="mini" label="身份证号" >
          <el-input size="mini" v-model="editForm.idCard" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item size="mini" label="性别" >
          <el-input size="mini" v-model="editForm.sex" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item size="mini" label="联系方式" >
          <el-input size="mini" v-model="editForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item size="mini" label="登陆密码" >
          <el-input size="mini" v-model="editForm.password" show-password autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubmit()">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
        height="560"
        v-loading="loading"
        element-loading-text="拼命加载中"
        :data="tableData"
        style="width: 100%;"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="学生ID">
              <span>{{ props.row.sid}}</span>
            </el-form-item>
            <el-form-item label="身份证号码">
              <span>{{ props.row.idCard}}</span>
            </el-form-item>
            <el-form-item label="地址">
              <span>{{ props.row.address}}</span>
            </el-form-item>
            <el-form-item label="联系方式">
              <span>{{ props.row.phone}}</span>
            </el-form-item>
            <el-form-item label="毕业学校">
              <span>{{ props.row.u_id}}</span>
            </el-form-item>

          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="姓名" prop="studentName"></el-table-column>
      <el-table-column label="性别" prop="sex"></el-table-column>
      <el-table-column label="年级" prop="yearNum"></el-table-column>
      <el-table-column align="right">
        <template slot="header">
          <input
              class="seach"
              @keyup.enter="searchHandle()"
              v-model="search"
              size="mini"
              placeholder="输入关键字搜索"
          />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br />
    <br />
    <br />
    <div style="float: right;margin-right: 10px" class="block">
      <el-pagination
          background
          hide-on-single-page
          @current-change="currentChange"
          layout="prev, pager, next"
          :current-page="page.current"
          :total="page.total"></el-pagination>
    </div>
  </div>
</template>

<script>
import { searchUnit, getUnitList, delUnit,getPageList,upStudent } from "../../network/admin";
import Loading from "../../components/common/Loading"
export default {
  name: "CompanyList",
  data() {
    return {
      dialogFormVisible:false,
      editForm:{
        sid:'',
        universityName:'',
        password:''
      },
      search: "",
      loading:true,
      page: {
        //后端传过来的数据,包含total(总条目),pages(总页数),records(数据),current(当前页号)
      }
    };
  },
  computed: {
    tableData() {
      console.log(this.page.records+"漂亮")
      return this.page.records;
    }
  },
  methods: {
    getData(){
      getUnitList("student")
          .then(res => {
            this.loading = false;
            this.page = res.data.extend.page;
            console.log("获取数据"+res)
            console.log(res)
          })
          .catch(err => {
            console.log(err);
          });
    },
    currentChange(val){
      getPageList('student',val)
          .then(res=>{
            this.page=res.data.extend.page
          })
    },
    change(e) {
      this.$forceUpdate();
    },
    searchHandle() {
      searchUnit("student", this.search).then(res => {
        console.log(res);
        this.page = res.data.extend.page;
      });
    },
    handleEdit(index,row){
      this.dialogFormVisible=true;
      this.editForm=row
    },
    editSubmit(){
      upStudent(this.editForm)
          .then(res=>{
            console.log(res)
            this.dialogFormVisible=false
            this.$message.success(res.data.msg)
          })
    },
    handleDelete(index, row) {
      delUnit("student", row.sid).then(res => {
        console.log(res)
        this.$message(res.data.extend.msg);
        this.getData()
      });
    }
  },
  created() {
    this.getData()
  }
};
</script>

<style scoped>
.seach{
  width: 240px;
  height: 24px;
  outline: none;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
